import { createStore } from 'vuex'

export default createStore({
  state: {
    products: [
      {
        id: 1,
        name: 'iPhone 13',
        price: 5999,
        originalPrice: 6799,
        discount: 12,
        image: 'https://picsum.photos/seed/iphone13/400/400',
        description: 'A new dual-camera system captures more of what you see and love.',
        details: '<p>6.1-inch Super Retina XDR display</p><p>Cinematic mode for recording videos with shallow depth of field</p><p>A15 Bionic chip with 6-core CPU</p><p>12MP dual-camera system with 12MP Ultra Wide and Wide cameras</p><p>Up to 19 hours of video playback</p>',
        reviews: 120,
        quantity: 1
      },
      {
        id: 2,
        name: 'MacBook Air',
        price: 7999,
        originalPrice: 9199,
        discount: 13,
        image: 'https://picsum.photos/seed/macbook/400/400',
        description: 'Light. Years ahead.',
        details: '<p>13.6-inch Liquid Retina display with P3 wide color and 500 nits brightness</p><p>M2 chip with 8-core CPU and 10-core GPU</p><p>Up to 24GB of unified memory</p><p>Up to 21 hours of battery life</p><p>1080p FaceTime HD camera</p>',
        reviews: 89,
        quantity: 1
      },
      {
        id: 3,
        name: 'iPad Pro',
        price: 6199,
        originalPrice: 6199,
        discount: 0,
        image: 'https://picsum.photos/seed/ipad/400/400',
        description: 'Supercharged by the Apple M1 chip.',
        details: '<p>11-inch Liquid Retina display with ProMotion</p><p>M1 chip with 8-core CPU and 8-core GPU</p><p>Up to 1TB of storage</p><p>12MP Ultra Wide front camera with Center Stage</p><p>LiDAR Scanner for immersive AR experiences</p>',
        reviews: 76,
        quantity: 1
      },
      {
        id: 4,
        name: 'Apple Watch Series 7',
        price: 2999,
        originalPrice: 3199,
        discount: 6,
        image: 'https://picsum.photos/seed/watch/400/400',
        description: 'The most advanced Apple Watch yet.',
        details: '<p>Brighter always-on Retina display</p><p>50% larger screen than Series 3</p><p>Advanced sensors for heart rate, ECG, and blood oxygen</p><p>Up to 18 hours of battery life</p><p>Water resistant up to 50 meters</p>',
        reviews: 156,
        quantity: 1
      },
      {
        id: 5,
        name: 'AirPods Pro',
        price: 1799,
        originalPrice: 1999,
        discount: 10,
        image: 'https://picsum.photos/seed/airpods/400/400',
        description: 'Active Noise Cancellation for immersive sound.',
        details: '<p>Active Noise Cancellation and Transparency mode</p><p>Adaptive EQ that automatically tunes music to the shape of your ear</p><p>Up to 4.5 hours of listening time with a single charge</p><p>MagSafe Charging Case with up to 24 hours of total listening time</p><p>Sweat and water resistant</p>',
        reviews: 201,
        quantity: 1
      },
      {
        id: 6,
        name: 'Apple TV 4K',
        price: 1799,
        originalPrice: 1799,
        discount: 0,
        image: 'https://picsum.photos/seed/appletv/400/400',
        description: 'Experience Apple TV in 4K.',
        details: '<p>A12 Bionic chip for stunning 4K HDR performance</p><p>Support for Dolby Vision, HDR10, and HLG</p><p>Up to 1080p camera and microphone for FaceTime calls</p><p>Apple TV app with access to thousands of movies and shows</p><p>Siri Remote with backlit touch surface</p>',
        reviews: 45,
        quantity: 1
      }
    ],
    cart: [],
    user: null,
    lastOrderDate: null
  },
  getters: {
    allProducts: (state) => state.products,
    getProductById: (state) => (id) => {
      return state.products.find(product => product.id === id)
    },
    cartItems: (state) => state.cart,
    cartItemsCount: (state) => {
      return state.cart.reduce((total, item) => total + item.quantity, 0)
    },
    cartSubtotal: (state) => {
      return state.cart.reduce((total, item) => total + (item.price * item.quantity), 0)
    },
    cartDiscount: (state) => {
      return state.cart.reduce((total, item) => {
        if (item.discount) {
          const discountAmount = (item.price / (1 - item.discount / 100)) - item.price
          return total + (discountAmount * item.quantity)
        }
        return total
      }, 0)
    },
    cartShipping: (state) => {
      const subtotal = state.cart.reduce((total, item) => total + (item.price * item.quantity), 0)
      return subtotal > 0 ? (subtotal >= 2999 ? '免费' : '¥18.00') : '¥0.00'
    },
    cartTotal: (state) => {
      const subtotal = state.cart.reduce((total, item) => total + (item.price * item.quantity), 0)
      const shipping = subtotal > 0 ? (subtotal >= 2999 ? 0 : 18) : 0
      return subtotal + shipping
    },
    isLoggedIn: (state) => !!state.user,
    lastOrderDate: (state) => state.lastOrderDate
  },
  mutations: {
    ADD_TO_CART(state, product) {
      const itemInCart = state.cart.find(item => item.id === product.id)
      if (itemInCart) {
        itemInCart.quantity += product.quantity || 1
      } else {
        state.cart.push({ ...product, quantity: product.quantity || 1 })
      }
      localStorage.setItem('cart', JSON.stringify(state.cart))
    },
    UPDATE_CART_ITEM(state, { id, quantity }) {
      const item = state.cart.find(item => item.id === id)
      if (item) {
        item.quantity = quantity
        localStorage.setItem('cart', JSON.stringify(state.cart))
      }
    },
    REMOVE_FROM_CART(state, id) {
      state.cart = state.cart.filter(item => item.id !== id)
      localStorage.setItem('cart', JSON.stringify(state.cart))
    },
    CLEAR_CART(state) {
      state.cart = []
      localStorage.setItem('cart', JSON.stringify(state.cart))
    },
    SET_USER(state, user) {
      state.user = user
      localStorage.setItem('user', JSON.stringify(user))
    },
    LOGOUT(state) {
      state.user = null
      localStorage.removeItem('user')
    },
    PLACE_ORDER(state) {
      state.lastOrderDate = new Date().toISOString()
      state.cart = []
      localStorage.removeItem('cart')
    }
  },
  actions: {
    addToCart({ commit }, product) {
      commit('ADD_TO_CART', product)
    },
    updateCartItem({ commit }, payload) {
      commit('UPDATE_CART_ITEM', payload)
    },
    removeFromCart({ commit }, id) {
      commit('REMOVE_FROM_CART', id)
    },
    clearCart({ commit }) {
      commit('CLEAR_CART')
    },
    login({ commit }, user) {
      commit('SET_USER', user)
    },
    logout({ commit }) {
      commit('LOGOUT')
    },
    },
    placeOrder({ commit }) {
      commit('PLACE_ORDER')
    }
  },
  modules,{
  }
,)    